/**
 *发布:
 * 1.判断是否登录:
 *    未登录:跳转至登录界面
 *    登录:继续下一步判断
 * 2.判断企业是否认证:
 * 企业未认证 : 跳转认证提示页面
 * 企业认证 : 直接显示供应发布页面
 */
import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    ListView,
    Image,
    TouchableOpacity,
    Platform,
    AsyncStorage,
    NetInfo,
    InteractionManager
} from 'react-native';
import Login from '../Account/Login';  //登录界面
import  SupplyPublish from './SupplyPublish';  //供应发布界面
import  CertificationHint from '../News/CertificationHint';  //认证提示界面
import  NotNetWork from  '../News/NotNetwork';   //无网络
import UserDefaults from '../common/UserDefaults'; //存值
import Common from '../common/constants'  //存值
import  CommitHint from  '../News/CommitHint'  //认证审核提示页
var certificationTag = 3//认证状态  =>  1:认证审核中  2:认证通过   3:认证失败   4:未认证
var netWorkTag = null //有无网络标示 true为有网络
import  CertificationFail from '../News/CertificationFail' //认证失败


class News extends  React.Component {
    render () {

        if (netWorkTag == false) {
            return (
                <NotNetWork />
            )
        } else{
            return (
                <SupplyPublish navigatorPush = {this.props.navigator} />
            )
        }
    }

    componentWillReceiveProps(){

    }

    /*
     *   收到移除网络状况,有网络把netWorkTag置为true,无网络置为false
     *   @param 无
     *   @returns 无
     * */
    handleMethod(isConnected){
        netWorkTag = isConnected
    }
    /*
     *   移除网络监听
     *   @param 无
     *   @returns 无
     * */
    componentWillUnmount() {
        // navigationContext.addEventListener('publish', )
        // this.props.navigator.navigationContext.removeListener('willfocus',()=>console.log('页面已经卸载'));
        NetInfo.isConnected.removeEventListener(
            'change',
            this.handleMethod
        );
    }
    /*
     *   视图加载前: 1.先判断有无网络,无网络显示无网络界面
     *              2.有网络再判断是否登录,没有登录,跳转至登录界面.
     *              3.已经是登录再判断有无认证,未认证跳转认证提示页
     *              4.认证过的话,显示发布页面
     *   @param 无
     *   @returns 无
     * */
    componentWillMount(){
        // this.props.navigator.navigationContext.addListener('willfocus',()=> console.log('这个新的过来都走'));


        //添加网络监听
        NetInfo.isConnected.addEventListener(
            'change',
            this.handleMethod
        );
        //判断有无网络+是否登录+是否认证
        if(netWorkTag == false){
            //无网络
            return;
        }else {
            //取出存的值
            UserDefaults.cachedObject(Common.storeKeys.LOGIN_SUCCESS_USERNAME_PSW_STATE)
                .then((loginInfo)=> {

                    if(loginInfo == null){
                        //取值为null,push到登录
                        InteractionManager.runAfterInteractions(()=>{
                            this.pushToXXX(Login,'Login')
                        })
                    }else {
                        //取值不为null,进行登录请求
                        var url =   'https://jijiuzaixian.com/login.json'  //admin  123123
                        var data = {"username":loginInfo[0],"password":loginInfo[1]};
                        var fetchOptions = {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json',
                            },
                            body: JSON.stringify(data)
                        };

                        fetch(url, fetchOptions)
                            .then((response) => response.json())
                            .then((json) => {
                                // console.log(json)
                                if(json.statusCode == '200'){
                                    //已经是登录,判断是否认证通过
                                    //认证状态  =>  1:认证审核中  2:认证通过   3:认证失败   4:未认证
                                    if(certificationTag == 2){
                                        //已经认证通过,显示发布页面
                                        return
                                    }else {
                                       //未认证通过
                                        if(certificationTag == 1){
                                            InteractionManager.runAfterInteractions(()=>{
                                                //认证审核中
                                                this.pushToXXX(CommitHint,'CommitHint')
                                            })
                                        }else if(certificationTag == 3){
                                            //认证失败
                                            this.pushToXXX(CertificationFail,'CertificationFail')
                                            return;
                                        }else {
                                            //未认证
                                            this.pushToXXX(CertificationHint,'NewsCertificationHint')
                                            return;
                                        }
                                    }
                                }else {
                                    //登录失败.push到登录
                                    InteractionManager.runAfterInteractions(()=>{
                                        //未认证.push到提示业
                                        this.pushToXXX(Login,'Login')
                                    })
                                }
                            })
                            .catch(error =>{
                                alert(error)
                            })
                            .done();
                    }

                })
        }

    }
    /*
     *
     *   push到相应页面
     *   @param
     *      name
     *      component
     *   @returns
     * */
    pushToXXX = (component,name) =>{
        //登录失败.push到登录
        this.props.navigator.push(
            {
                name:name,
                component: component,
                passProps: {

                }
            }
        );
    }

}



var styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F5FCFF',
        justifyContent:'center',
        alignItems:'center'
    }
});

module.exports = News;
